<?php
/**
 * Created by PhpStorm.
 * User: huangzikun
 * Date: 2018/4/19
 * Time: 17:04
 */
namespace Huangzikun\hawkeye_spider;

class CpuSpiderClass extends BaseClass
{
    private $filePath = "/proc/cpuinfo";
    public $spiderInfo;
    public $apiAddress;

    public function __construct($apiAddress)
    {
        parent::__construct($apiAddress);
    }

    /**
     * cpu信息抓取
     */
    public function grab()
    {
        $info = file($this->filePath);
        if(!empty($info) && is_array($info)){
            $i = -1;
            foreach ($info as $str){
                $str = str_replace("\n", "", $str);
                $tmp = explode(":", $str);
                if(count($tmp) == 2){
                    //cpu逻辑核心
                    if(trim($tmp[0]) == 'processor'){
                        $i++;
                    }
                    if(trim($tmp[0]) == 'cache size'){
                        $tmp[1] = explode(" ", trim($tmp[1]))[0];
                    }
                    $this->spiderInfo[$i][trim($tmp[0])] = trim($tmp[1]);
                }
            }
        }
    }
}